﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PlayingCardLib;

using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace CardDealer
{
    public partial class Form1 : Form
    {
        const int NUM_CARDS = 13;
        const int NUM_COLS = 5;
        const int CARD_WIDTH = 75;
        const int CARD_HEIGHT = 107;
        const int GAP_X = 20;
        const int GAP_Y = 20;

        Deck deck;
        PictureBox[] cards;

        private Image back;
        private List<Image> faces = new List<Image>();

        // private PlayingCard[,] hands = new PlayingCard[4, NUM_CARDS];
        private Hand[] hands = new Hand[4];

        static IFormatter bfmt = new BinaryFormatter();

        public Form1()
        {
            InitializeComponent();
        }

        
        private void Form1_Load(object sender, EventArgs e)
        {
            for(int j = 0; j < 4; j++)
            {
                hands[j] = new Hand();
            }
            for (int i = 0; i <= 51; i++)
            {
                faces.Add(cardImages.Images[i]);
            }
            back = cardImages.Images[56];

            DealCards();

            // create picture boxes
            cards = new PictureBox[NUM_CARDS];
            for (int i = 0; i < NUM_CARDS; i++)
            {
                cards[i] = new PictureBox();
                cards[i].Width = CARD_WIDTH;
                cards[i].Height = CARD_HEIGHT;
                int col = i % NUM_COLS;
                int row = i / NUM_COLS;
                cards[i].Location = new Point(col * (GAP_X + CARD_WIDTH) + 120, row * (GAP_Y + CARD_HEIGHT) + 40);
                this.Controls.Add(cards[i]);
            }
            // It will choose the first hand, because I set the radio checked?
        }  // inital form

        private void DealCards()
        {
            for(int j = 0; j < 4; j++)
            {
                if(hands[j].Cards.Count() > 0)
                {
                    hands[j].Cards.Clear();
                }
                
            }
            deck = new Deck();
            deck.AssignImages(faces, back);
            deck.Shuffle();

            // assign four hands
            for (int i = 0; i < 13; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    hands[j].Cards.Add(deck.DealTopCard());
                }
            }
        } // Deal Cards

        private void ShowHand(int handIndex)
        {
            
            for (int i = 0; i < NUM_CARDS; i++)
            {
                PlayingCard card = hands[handIndex].Cards[i];
                card.IsFaceUp = true;
                cards[i].Image = card.GetImage();
                cards[i].Tag = card;
                cards[i].Refresh();
            }
        }

        private void radioButtonHandOne_Click(object sender, EventArgs e)
        {
            ShowHand(0);
        }

        private void radioButtonHandTwo_Click(object sender, EventArgs e)
        {
            ShowHand(1);
        }

        private void radioButtonHandThree_Click(object sender, EventArgs e)
        {
            ShowHand(2);
        }

        private void radioButtonHandFour_Click(object sender, EventArgs e)
        {
            ShowHand(3);
        }

        private void buttonDeal_Click(object sender, EventArgs e)
        {
            DealCards();

            radioButtonHandOne.Checked = true;
            ShowHand(0);

        } // Deal Button

        private void buttonSave_Click(object sender, EventArgs e)
        {
            Stream stream;
            SaveFileDialog saveFileDialogHands = new SaveFileDialog();

            // saveFileDialogHands.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            saveFileDialogHands.Filter = "hand of cards (*.hand)|*.hand|All files (*.*)|*.*";
            saveFileDialogHands.FilterIndex = 1;
            saveFileDialogHands.RestoreDirectory = true;

            if(saveFileDialogHands.ShowDialog() == DialogResult.OK)
            {
                if((stream = saveFileDialogHands.OpenFile()) != null)
                {
                    // Code to write the stream goes here.
                    for(int j=0; j < 4; j++)
                    {
                        bfmt.Serialize(stream, hands[j]);
                    }
                    stream.Close();
                }
            }
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            var filePath = string.Empty;  // What does this mean?

            using(OpenFileDialog openFileDialogHands = new OpenFileDialog())
            {
                openFileDialogHands.InitialDirectory = "C:\\";
                openFileDialogHands.Filter = "hand of cards (*.hand)|*.hand|All files (*.*)|*.*";
                openFileDialogHands.FilterIndex = 1;
                openFileDialogHands.RestoreDirectory = true;

                if(openFileDialogHands.ShowDialog() == DialogResult.OK)
                {
                    // Get the path of specified file
                    filePath = openFileDialogHands.FileName;

                    // Read the contents of the file into a stream
                    // var fileStream = openFileDialogHands.OpenFile();
                    // https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.openfiledialog?view=netframework-4.8

                    using (Stream stream = File.OpenRead(filePath))
                    {
                        
                        for(int j=0;j<4;j++)
                        {
                            hands[j] = bfmt.Deserialize(stream) as Hand;
                            
                        }
                        
                        stream.Close();

                    }

                }
            }

            radioButtonHandOne.Checked = true;
            ShowHand(0);
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
